<?php
/**
 * $Id$
 */
class WikiClass
{
	private $_dbName = 'community';
	private $_serverID = 5;
	
	public function __construct()
	{
		
	}
	
	public function addWiki($title, $content, $type = 1)
	{
		$type = intval($type);
		if(mEmpty($title, $content, $type))
		{
			return false;
		}
		$sql = "INSERT INTO {$this->_dbName}.wikiContentTbl SET content='{!{$content}!}'";
		db_query_sql($sql, $this->_serverID);
		$contentID = db_get_insert_id();
		if(empty($contentID))
		{
			return false;
		}
		$sql = "INSERT INTO {$this->_dbName}.wikiTitleTbl (`type`, title, `addTime`, contentID)VALUES({$type}, '{!{$title}!}', ".G_CURR_TIMESTAMP.", $contentID)";
		db_query_sql($sql, $this->_serverID);
		$titleID = db_get_insert_id();
		if($titleID)
		{
			$sql = "UPDATE {$this->_dbName}.wikiContentTbl SET titleID={$titleID} WHERE ID={$contentID}";
			db_query_sql($sql, $this->_serverID);
			return db_get_affected_rows();
		}
		return false;
	}
	
	public function getWikiList($titleIDS = '', $getCount = false, $orderBy = 'A.addTime DESC', $limit = '0,20')
	{
		$titleIDS = getQueryInStr($titleIDS);
		$sql = "SELECT {FIELD} FROM {$this->_dbName}.wikiTitleTbl A INNER JOIN {$this->_dbName}.wikiContentTbl B ON A.ID=B.titleID WHERE 1";
		$sql = str_replace('{FIELD}', $getCount ? 'COUNT(*) c' : 'A.*, B.content, B.changeTime, B.viewNumber', $sql);
		$sql .= empty($titleIDS) ? '' : " AND A.ID IN({$titleIDS})";
		$sql .= ' ORDER BY '.(empty($orderBy) ? ' addTime DESC' : $orderBy);
		$sql .= empty($limit) ? '' : ' LIMIT '.$limit;
		$___rs = array();
		
		$rs = db_query_sql($sql, $this->_serverID, 7200);
		if($getCount)
		{
			return $rs[0]['c'];
		}
		if(empty($titleIDS))
		{
			return $rs;
		}
		foreach($rs as $val)
		{
			$___rs[$val['ID']] = $val;
		}
		return $___rs;
	}
	
	public function changWiki($titleID, $title, $content)
	{
		$titleID = intval($titleID);
		if($titleID < 1)
		{
			return false;
		}
		$info = $this->getWikiList($titleID);
		if(empty($info[$titleID]))
		{
			return false;
		}
		$info = $info[$titleID];
		$sql = "UPDATE {$this->_dbName}.wikiTitleTbl A, {$this->_dbName}.wikiContentTbl B SET A.title='{!{$title}!}', B.content='{!{$content}!}', B.changeTime='{$_SERVER['REQUEST_TIME']}' WHERE A.ID={$titleID} AND A.ID=B.titleID";
		db_query_sql($sql, $this->_serverID);
		return db_get_affected_rows();
	}

	
}